package com.infonam.media.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.infonam.media.R;
import com.infonam.media.database.DatabaseHelper;
import com.infonam.media.database.entity.User;

public class LoginActivity extends Activity {

	private Button loginbtn;
	private EditText username;
	private EditText password;
	private TextView error;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		loginbtn = (Button) findViewById(R.id.btnLogin);
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		error = (TextView) findViewById(R.id.login_error);

		// Listening to button login
		loginbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if ((username.getText().toString().length() == 0)) {
					error.setText("Username is required");

				} else if ((password.getText().toString().length() == 0)) {
					error.setText("Password is required");
				} else {
					DatabaseHelper dbhelper = new DatabaseHelper(
							getApplicationContext());
					if (dbhelper.checkUser(username.getText().toString(),
							password.getText().toString())) {

						error.setText("Welcome "
								+ username.getText().toString());

						Intent intent = new Intent(LoginActivity.this,
								ListVideoActivity.class);
						startActivity(intent);
					} else
						error.setText("Wrong userID or pass ");
				}

			}
		});

		// put user data into DB
		DatabaseHelper dbhelper = new DatabaseHelper(getApplicationContext());
		if (dbhelper.getUser("user01") == null) {
			Log.d("Insert: ", "Inserting ..");
			dbhelper.addUser(new User("user01", "123456"));
			dbhelper.addUser(new User("user02", "123456"));
			dbhelper.addUser(new User("user03", "123456"));
			dbhelper.addUser(new User("user04", "123456"));
		}

	}

	@Override
	public void onDestroy() {
		super.onDestroy(); // Always call the superclass

	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}
}